<?php

namespace App\Admin\Controllers;

use App\Model\Category;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Tree;

class CategoryController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('Index')
            ->description('description')
            ->body($this->tree());
    }

    public function tree(){
        return Category::tree(function (Tree $tree) {

            $tree->branch(function ($branch) {
                $isShow = $branch['is_show'] == '1' ? '显示' : '隐藏';
                return "{$branch['sort']} - {$branch['title']} - {$isShow} ";
            });
        });
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Category);

        $grid->id('编号');
        $grid->pid('Pid');
        $grid->title('标题');
        $grid->is_show('Is show');
        $grid->created_at('Created at');
        $grid->updated_at('Updated at');

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Category::findOrFail($id));

        $show->id('Id');
        $show->pid('Pid');
        $show->title('Title');
        $show->is_show('Is show');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Category);

        $form->select('pid','上级')->options(Category::selectOptions());

        $form->text('title', '标题');
        $form->uploadimg('icon_upload_id', '图标');

        $form->text('sort', '排序');
        $states = [
            'on'  => ['value' => '1', 'text' => '显示', 'color' => 'success'],
            'off' => ['value' => '2', 'text' => '隐藏', 'color' => 'danger'],
        ];
        $form->switch('is_show', '是否显示')->states($states);

        return $form;
    }
}
